package com.service.gzp.impl;

import com.dao.RoleDao;
import com.dao.RoletreeDao;
import com.dao.UserinforoleDao;
import com.entity.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.gzp.RoleService;
import com.util.gzp.AjaxResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: RoleServiceImpl
 * Package: com.service.gzp.impl
 *
 * @author: guanzepeng
 * @Date: 2021/4/8 14:20 星期四
 * @Description: 用户角色权限业务层
 */
@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleDao roleDao;

	@Autowired
	private RoletreeDao roletreeDao;

	@Autowired
	private UserinforoleDao userinforoleDao;

	/**
	 * 新增的时候查询所有角色
	 * @return 返回数据
	 */
	@Override
	public AjaxResponse findBy() {
		List<Role> roles = roleDao.selectByExample(null);
		return AjaxResponse.success(roles);
	}

	/**
	 * 查询所有的角色
	 * @param rname 传入角色名称 模糊查询
	 * @param rid 传入角色ID 进行单条查询
	 * @param pageNum 传入分页数据
	 * @param pageSize 传入分页数据
	 * @return 返回角色信息
	 */
	@Transactional
	@Override
	public AjaxResponse findByRole(String rname, Integer rid, Integer pageNum, Integer pageSize) {
		RoleExample roleExample = new RoleExample();
		RoleExample.Criteria criteria = roleExample.createCriteria();
		//如果rid不为空的话就是查询单条角色数据
		if(null != rid) {
				criteria.andRidEqualTo(rid);
			List<Role> roles = roleDao.selectByExample(roleExample);
			if (roles.size() != 1) {
				return AjaxResponse.success("查询有误,请重新输入!",500);
			}
			return AjaxResponse.success(roles.get(0));
		}
		//模糊查询角色名称
		if(null != rname && rname.length() != 0 && !"".equals(rname)) {
			criteria.andRnameLike("%" + rname + "%");
		}
		PageHelper.startPage(pageNum,pageSize);
		List<Role> roles = roleDao.selectByExample(roleExample);
		PageInfo<Role> rolePageInfo = new PageInfo<>(roles);
		return AjaxResponse.success(rolePageInfo);
	}

	/**
	 * 新增一个角色
	 * @param role 传入角色数据
	 * @return 返回成功失败
	 */
	@Override
	@Transactional
	public AjaxResponse insertRole(Role role) {
		if(null == role || role.getRname().length() == 0) {
			return AjaxResponse.success("输入有误,新增失败!",500);
		}
		boolean b = roleDao.insertSelective(role) == 1;
		if(!b) {
			return AjaxResponse.success("新增失败!",500);
		}
		//添加登录以及修改密码权限
		RoletreeKey roletreeKey = new RoletreeKey();
		roletreeKey.setRid(role.getRid());
		roletreeKey.setTid(9);
		b = roletreeDao.insertSelective(roletreeKey) == 1;
		if(!b) {
			return AjaxResponse.success("新增失败!",500);
		}
		roletreeKey.setTid(36);
		b = roletreeDao.insertSelective(roletreeKey) == 1;
		if(!b) {
			return AjaxResponse.success("新增失败!",500);
		}
		return AjaxResponse.success();
	}

	/**
	 * 修改一个角色的信息
	 * @param role 传入要修改的信息,用户ID为必填参数
	 * @return 返回成功失败
	 */
	@Override
	public AjaxResponse updateRole(Role role) {
		if(null == role || null == role.getRid() || 0 == role.getRname().length()) {
			return AjaxResponse.success("输入有误,修改失败!",500);
		}
		RoleExample roleExample = new RoleExample();
		RoleExample.Criteria criteria = roleExample.createCriteria();
		criteria.andRidEqualTo(role.getRid());
		boolean b = roleDao.updateByExampleSelective(role, roleExample) == 1;
		if(!b) {
			return AjaxResponse.success("修改失败!",500);
		}
		return AjaxResponse.success();
	}

	/**
	 * 删除一个角色
	 * @param rid 传入角色ID
	 * @return 返回成功失败
	 */
	@Override
	public AjaxResponse deleteRole(Integer rid) {
		if(null == rid) {
			return AjaxResponse.success("请传入角色ID", 500);
		}
		boolean b = roleDao.deleteByPrimaryKey(rid) == 1;
		if(!b) {
			return AjaxResponse.success("删除失败!",500);
		}
		return AjaxResponse.success();
	}

	/**
	 * 查询用户的当前角色
	 * @param loginId uid
	 * @return 返回角色List集合
	 */
	@Override
	public List<String> findByR(Integer loginId) {
		if(null == loginId) {
			return new ArrayList<>();
		}
		UserinforoleExample userinforoleExample = new UserinforoleExample();
		UserinforoleExample.Criteria criteria = userinforoleExample.createCriteria();
		criteria.andUidEqualTo(loginId);
		List<UserinforoleKey> userinforoleKeys = userinforoleDao.selectByExample(userinforoleExample);
		if(userinforoleKeys.size() == 0) {
			return new ArrayList<>();
		}
		List<Integer> list = new ArrayList<>();
		RoleExample roleExample = new RoleExample();
		RoleExample.Criteria criteria1 = roleExample.createCriteria();
		for (UserinforoleKey u : userinforoleKeys) {
			list.add(u.getRid());
		}
		criteria1.andRidIn(list);
		List<Role> roles = roleDao.selectByExample(roleExample);
		List<String> l = new ArrayList<>();
		for (Role r : roles) {
			l.add(r.getRname());
		}
		return l;
	}


}
